// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.standard.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.Specification;
import com.liuxinlong.modules.standard.controller.request.QuerySpecificationReq;
import com.liuxinlong.modules.standard.controller.request.UpdateSpecificationReq;
import com.liuxinlong.modules.standard.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设计规格书控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年3月3日
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {
	@Autowired
	private SpecificationService specificationService;

	/**
	 * 分页查询设计规格书信息
	 * 
	 * @param req 请求信息
	 * @return 设计规格书信息列表
	 */
	@PostMapping("page")
//	@LogAnnotation(value = "分页查询", business = LogBusinessConstants.DESIGN_SPECIFICATION,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageSpecificationInfo(@RequestBody QuerySpecificationReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("specificationSn", req.getSpecificationSn());
		queryMap.put("name", req.getName());
		queryMap.put("applicationUser", req.getApplicationUser());
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		List<Map<String, Object>> dataList = specificationService.pageSpecificationInfo(queryMap);
		int totalNum = specificationService.getSpecificationCount(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询设计规格书信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(totalNum);
		return resp;
	}

	/**
	 * 新增设计规格书信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增设计规格书信息",business = LogBusinessConstants.DESIGN_SPECIFICATION,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addSpecificationInfo(@RequestBody UpdateSpecificationReq req) {
		Specification specification = new Specification();
		specification.setName(req.getName());
		specification.setSpecificationSn(req.getSpecificationSn());
		specification.setSecurityLevel(req.getSecurityLevel());
		specification.setApplicability(req.getApplicability());
		specification.setAttach(req.getAttach());
		specification.setRemark(req.getRemark());
		specificationService.addSpecificationInfo(specification);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增设计规格书信息成功");
		return resp;

	}

	/**
	 * 修改设计规格书信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="更新设计规格书信息",business = LogBusinessConstants.DESIGN_SPECIFICATION,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateSpecificationInfo(@RequestBody UpdateSpecificationReq req) {
		Specification specification = new Specification();
		specification.setId(req.getId());
		specification.setName(req.getName());
		specification.setSecurityLevel(req.getSecurityLevel());
		specification.setVersion(req.getVersion());
		specification.setApplicability(req.getApplicability());
		specification.setAttach(req.getAttach());
		specification.setRemark(req.getRemark());
		specificationService.updateSpecificationInfo(specification);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改设计规格书信息成功");
		return resp;
	}

	/**
	 * 删除设计规格书信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除设计规格书信息",business = LogBusinessConstants.DESIGN_SPECIFICATION,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteSpecificationInfo(@RequestBody BaseDeleteReq req) {
		specificationService.deleteSpecificationInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除设计规格书信息成功");
		return resp;
	}

	/**
	 * 导出设计规格书信息
	 * 
	 * @param req 请求信息
	 * @return 导出数据
	 */
	@PostMapping("export")
	@LogAnnotation(value ="导出设计规格书信息",business = LogBusinessConstants.DESIGN_SPECIFICATION,operateType = LogOperateConstants.OPERATE_EXPORT)
	public BaseResp exportSpecificationInfo(@RequestBody @Valid BaseDeleteReq req) {
		specificationService.exportSpecificationInfo(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("导出设计规格书信息成功");
		return resp;
	}

}
